//
//  HMHomeToolBar.swift
//  Weibo
//
//  Created by itteacher on 2017/7/26.
//  Copyright © 2017年 itteacher. All rights reserved.
//

import UIKit
import SnapKit

//  首页cell的toolBar
class HMHomeToolBar: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        let retweetButton = addChildButton(imageName: "timeline_icon_retweet", title: "转发")
        let commnetButton = addChildButton(imageName: "timeline_icon_comment", title: "评论")
        let unlikeButton = addChildButton(imageName: "timeline_icon_unlike", title: "赞")
        
        //  添加约束
        retweetButton.snp.makeConstraints { (make) in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(commnetButton)
        }
        commnetButton.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(retweetButton.snp.trailing)
            make.width.equalTo(unlikeButton)
        }
        unlikeButton.snp.makeConstraints { (make) in
            make.top.trailing.bottom.equalTo(self)
            //  关键约束
            make.leading.equalTo(commnetButton.snp.trailing)
        }
        
        //  第一条条线
        let firstLineView = addLineView()
        //  第二条条线
        let secondLineView = addLineView()
        
        //  添加约束
        firstLineView.snp.makeConstraints { (make) in
            make.centerX.equalTo(retweetButton.snp.trailing)
            make.centerY.equalTo(retweetButton)
        }
        secondLineView.snp.makeConstraints { (make) in
            make.centerX.equalTo(commnetButton.snp.trailing)
            make.centerY.equalTo(commnetButton)
        }
    }
    
    //  添加子按钮
    private func addChildButton(imageName: String, title: String) -> UIButton {
        let button = UIButton()
        //  设置图片
        button.setImage(UIImage(named: imageName), for: .normal)
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), for: .normal)
        //  设置文字
        button.setTitle(title, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        //  取消高亮点击
        button.adjustsImageWhenHighlighted = false
        //  添加按钮
        addSubview(button)
        return button
    }
    
    //  添加竖线视图
    private func addLineView() -> UIImageView {
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        //  添加子视图
        addSubview(imageView)
        return imageView
    }
    
    
    
    
}
